package com.example.mynote.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mynote.R;
import com.example.mynote.base.BaseActivity;
import com.example.mynote.entity.StickyNote;

import java.util.Date;

public class AddNoteActivity extends BaseActivity {

    private android.widget.ImageButton addBack;
    private android.widget.TextView addText;
    private android.widget.ImageButton addSave;
    private android.widget.EditText stickNote;
    private android.widget.TextView stickNum;

    @Override
    protected int initLayout() {
        return R.layout.activity_add_note;
    }

    @Override
    protected void initView() {

        addBack = (ImageButton) findViewById(R.id.add_back);
        addText = (TextView) findViewById(R.id.add_text);
        addSave = (ImageButton) findViewById(R.id.add_save);
        stickNote = (EditText) findViewById(R.id.stick_note);
        stickNum = (TextView) findViewById(R.id.stick_num);
    }

    @Override
    protected void initData() {
        addBack.setOnClickListener(v -> onBackPressed());
        addText.setText("新建");

        addSave.setOnClickListener(v -> {
            String stick = stickNote.getText().toString();
            if (!stick.equals("")) {
                new StickyNote(stick, new Date().getTime()).save();
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            }
        });

        stickNote.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                stickNum.setText(s.length() + "字");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}